#include "iostream"
#include "fstream"
#include "list"
using namespace std;
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

static char* s_cGlobalPlayer1;
static char* s_cGlobalPlayer2;
static char s_cGlobalBoard[3][3];
static int s_iGlobalSizeBoard = 3;
static bool s_bGlobalSwithPlayer;
static vector<string> s_lSaveBoard;
void SwitchPlayer();
void Tictactoe();
void InputNamePlayer();
void InGame();
void ShowBoard();
void LoadDataSave();
void ShowReplay();
int CheckColumn(char a);
int CheckRow(char a);
int CheckCrossIncrease(char a);
bool IsCheckWon(char a);
int CheckMove(char a);
void SwitchPlayer()
{
	int localMode;
	printf("\n SELECT YOUR MODE (1 -PLAY GAME, 2 - REPLAY GAME, OTHERS - EXIT GAME):");
	scanf("%d", &localMode);
	switch(localMode)
	{
	case 1 : InputNamePlayer();
			 ShowBoard();
			 InGame();
			break;
	case 2 : 

			break;
	default : exit (EXIT_FAILURE);
	}
}
void ReplayGame()
{	 
	FILE *fp;
  char line[100];
  int count=0,i;
   
 char names[100];
 fp = fopen ("D:\\Names.txt","r");
 while(feof(fp)==0) 
   {
     fgets(names,100,fp)  ;
     count++;
     printf("%s",names);
   }    
 printf("File contains %d lines.\n", count-1);
 char* temp ;
 int j = 0;
 while(names[j]!='\0')
 {
	 temp[j]=names[j];
	 j++;
	 if((j+1)%9 == 0)
	 {
		string temp1 = 
	 }

 }
 
 cout<<"TEMP"<<j;
fclose(fp);    
}
  
void InGame()
{
	char* localPosition = new char[];
	while(true)
	{
		cout << "\n Your turn "<<(s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2)<<(s_bGlobalSwithPlayer ?" O:" : " X:" );
		cin >> localPosition;
		int i = localPosition[0] - '0';
		int j = localPosition[1] - '0';
		if(strlen(localPosition)>=3 || i >= (s_iGlobalSizeBoard)*(s_iGlobalSizeBoard) || j >= (s_iGlobalSizeBoard)*(s_iGlobalSizeBoard) || strlen(localPosition)<=1 )
		{
			cout << "\n You went out the board";
			
			
		}
		else
		{
			i = i -1;
			j = j -1;
			if(s_bGlobalSwithPlayer)
			{
				if(s_cGlobalBoard[i][j] != 'O' && s_cGlobalBoard[i][j] != 'X')
				{
					s_cGlobalBoard[i][j] = 'O';
					s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;
				}
				else 
				{
					cout << "This move invalid!\n";

				}
				if(IsCheckWon('O'))
				{
					cout << "Congratulation "<<(!s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2);
					cout << "\nYou won!!!";
					system("pause");
					break;
				}	
			}
			else
			{
				if(s_cGlobalBoard[i][j] != 'O' && s_cGlobalBoard[i][j] != 'X')
				{
					s_cGlobalBoard[i][j]  = 'X';
					s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;
				}
				else 
				{
					cout << "This move invalid!\n";
				}
				if(IsCheckWon('X'))
				{
					cout << "Congratulation "<<(!s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2);
					cout << "\nYou won!!!";
					system("pause");
					break;
				}
			}
			ShowBoard();
			

		}
	}

}

void ShowBoard()
{
	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			cout << s_cGlobalBoard[i][j]<<" ";
		cout<<"\n";
	}
}
void InputNamePlayer()
{
	cout << "Enter Player 1 Name:";
	cin >> s_cGlobalPlayer1 ;
	cout << "Enter Player 2 Name:";
	cin >> s_cGlobalPlayer2 ;
	//printf("s_cGlobalPlayer1=%x",&s_cGlobalPlayer1);
	cout << "Name1="<<s_cGlobalPlayer1<<endl;
	cout << "Name2="<<s_cGlobalPlayer2<<endl;
	
}
bool IsCheckWon(char a)
{
	if(CheckRow(a)||CheckColumn(a)||CheckCrossIncrease(a))
	return true;
	else return false;

}
int CheckRow(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[i][j] == a)
			{
				if(s_cGlobalBoard[i][j+1] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;
}
int CheckColumn(char a)
{
	int localCount = 0;
	int localResult = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		for(int j = 0; j < s_iGlobalSizeBoard; j++)
			if(s_cGlobalBoard[j][i] == a)
			{
				if(s_cGlobalBoard[j+1][i] == a)
				{
					localCount += 1;
				}
			}
			if(localCount == 2)
			{
				localResult = 1;
				break;
			}
			else localResult = 0;
	}
	return localResult;

}
int CheckCrossIncrease(char a)
{
	int localResult = 0;
	int localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][s_iGlobalSizeBoard -i - 1] == a )
			if(s_cGlobalBoard[i][s_iGlobalSizeBoard - 1 - i ] == a )
			localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;
	}
	localCount = 0;
	for(int i = 0; i < s_iGlobalSizeBoard; i++)
	{
		if(s_cGlobalBoard[i][i] == a)
			if(s_cGlobalBoard[i+1][i+1] == a)
				localCount++;
		if(localCount == 3)
		{
			localResult = 1;
			break;
		}
		else localResult = 0;

	}
	return localResult;


}
void Tictactoe()
{

	for(int i = 0 ; i < s_iGlobalSizeBoard ; i ++ )
		for(int j = 0; j < s_iGlobalSizeBoard; j ++)
			s_cGlobalBoard[i][j] = 'T';
	s_bGlobalSwithPlayer = true;
	
	s_cGlobalPlayer1 = new char[];
	s_cGlobalPlayer2 = new char[];
	

}
int main()
{
	printf("Welcome To Tic-tac-toe game! Play with your way!");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("If you find any problem, please contact tho.lekhac@gameloft.com\n");
//Add code
	Tictactoe();
	//InputNamePlayer();
	//ShowBoard();
	//SwitchPlayer();
	ReplayGame();
	printf("\n");
	printf("\n");
	printf("\n");
	cin.get();
}